import {Body, Controller, Delete, Get, Param, Post, Put, Query, Request} from "@nestjs/common";
import {ApiBody, ApiOperation, ApiQuery, ApiTags} from "@nestjs/swagger";
import {KnowledgeGraphService} from "./knowledgeGraph.service";
import {Public} from "../../common/decorators/public.decorators";
import {PagingRequestDto} from "../../common/dto/paging.dto";
import {UserActions} from "../../common/enums/userActions";
import {UserActionService} from "../common/userAction/userAction.service";
import {randomNodePropertyDto} from "./knowledgeGraph.dto";

@ApiTags("知识图谱")
@Controller("knowledgeGraph")
export class KnowledgeGraphController {

    constructor(
        private readonly recommendService: KnowledgeGraphService,
        private readonly userActionService: UserActionService
    ) {
    }

    @Public()
    @Get("getBookNodeByPage")
    @ApiOperation({summary: "查询图书节点(分页)"})
    @ApiQuery({
        type: PagingRequestDto
    })
    async getBookList(
        @Query() query: PagingRequestDto
    ) {
        const {pageNo, pageSize} = query;
        return await this.recommendService.getBookNodeList(pageNo, pageSize);
    }

    @Public()
    @Get("getNodeProperty")
    @ApiOperation({summary: "随机获取指定节点"})
    @ApiQuery({
        type: randomNodePropertyDto
    })
    async getNodePropertyListRandom(
        @Query() query: randomNodePropertyDto
    ) {
        const {nodeName,size} = query;
        return await this.recommendService.getNodePropertyListRandom(nodeName,Number(size));
    }


    @Get("getBookNode")
    @ApiOperation({summary: "根据图书id查询图书节点"})
    @ApiQuery({
        name: "bookId",
        type: String
    })
    async getBook(
        @Request() req,
        @Query("bookId") bookId: string
    ) {
        const {userId} = req.user;
        const result = await this.recommendService.getBookNodeById(userId,bookId);
        if (result){
            await this.userActionService.insert({userId, action: UserActions.HAS_READ, bookId});
        }
        return result

    }

    @ApiOperation({summary: "查询用户->图书的关系"})
    @Get("findRelationshipToBook")
    async findRelationshipToBook(
        @Request() req,
        @Query("bookId") bookId: string
    ) {
        const {userId} = req.user;
        return await this.recommendService.getBookRelationshipByUser(userId, bookId);
    }

    @ApiOperation({summary: "创建用户节点"})
    @Post("addUserNode")
    async addUserNode(
        @Request() req
    ) {
        const {username, userId} = req.user;
        return await this.recommendService.createUserNode(username, userId);
    }

    // @ApiOperation({summary: "保存用户与图书的关系(看过)"})
    // @Put("createRelationshipToBook/:bookId")
    // async createRelationshipToBook(
    //     @Request() req,
    //     @Param("bookId") bookId: string
    // ) {
    //     const {userId} = req.user;
    //     return await this.recommendService.saveRelationshipFromUserToBookRead(userId, bookId);
    // }

    @ApiOperation({summary: "保存用户与图书的关系(收藏)"})
    @Put("createRelationshipToBookCollection/:bookId")
    async createRelationshipToBookCollection(
        @Request() req,
        @Param("bookId") bookId: string
    ) {
        const {userId} = req.user;
        return await this.recommendService.saveRelationshipFromUserToBookCollection(userId, bookId);
    }

    @ApiOperation({summary: "删除用户与图书的关系(收藏)"})
    @Delete("deleteRelationshipToBookCollection/:bookId")
    async deleteRelationshipToBookCollection(
        @Request() req,
        @Param("bookId") bookId: string
    ) {
        const {userId} = req.user;
        return await this.recommendService.deleteRelationshipFromUserToBookCollection(userId, bookId);
    }

}
